<template>
  <div class="box-item" :class="{ 'box-border': hasBorder }">
    <div v-if="hasHeader" class="item-header">
      <div class="header-left">{{ title }}</div>
      <div v-if="hasRight" class="header-right">
        <span>{{ rightText }}</span>
      </div>
    </div>
    <div class="item-body" :class="{ 'item-noheader': !hasHeader }">
      <slot name="body">
        <div v-if="isChart" class="slot-area">
          <slot name="chartDecoration" />
        </div>
        <div v-if="isChart" ref="chartBox" class="chart-box" />
        <div v-else class="table-box">
          <el-table :data="tableData" border stripe size="mini" height="100%">
            <el-table-column
              type="index"
              label="序号"
              width="70"
              show-overflow-tooltip
              align="center"
            />
            <el-table-column
              v-for="col in tableColumns"
              :prop="col.prop"
              :key="col.id"
              :label="col.label"
              :width="col.width"
              show-overflow-tooltip
              align="center"
            >
            </el-table-column>
          </el-table>
        </div>
      </slot>
    </div>
  </div>
</template>

<script>
import * as echarts from "echarts";
export default {
  props: {
    isCustom: {
      type: Boolean,
      default: false,
    },
    hasBorder: {
      type: Boolean,
      default: true,
    },
    hasHeader: {
      type: Boolean,
      default: true,
    },
    isChart: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      default: () => ({}),
    },
    title: {
      type: String,
      default: "默认标题",
    },
    hasRight: {
      type: Boolean,
      default: false,
    },
    rightText: {
      type: String,
      default: "查看更多",
    },
    tableData: {
      type: Array,
      default: () => [],
    },
    tableColumns: {
      type: Array,
      default: () => [],
    },
    headerHeight: {
      type: Number,
      default: 40,
    },
  },
  data() {
    return {};
  },
  created() {},
  mounted() {
    this.isCustom || (this.isChart ? this.initChart() : this.initTable());
  },

  methods: {
    // 初始化图表
    initChart() {
      // 基于准备好的dom，初始化echarts实例
      const myChart = echarts.init(this.$refs.chartBox);
      // 绘制图表
      myChart.setOption(this.chartData);
    },
    // 初始化表格
    initTable() {},
  },
};
</script>
<style  scoped lang="scss">
.box-item {
  box-sizing: border-box;
  width: 100%;
  height: 100%;
  background: #fff;
  .item-header {
    position: relative;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 50px;
    border-bottom: 1px solid #e8e8e8;
    background: transparent;
    .header-left {
      text-indent: 24px;
      &::before {
        content: "";
        position: absolute;
        left: 16px;
        top: 50%;
        width: 3px;
        height: 10px;
        transform: translateY(-50%);
        background: #3370ff;
      }
    }
  }
  .item-body {
    position: relative;
    height: calc(100% - 50px);
  }
  .item-noheader {
    height: 100%;
  }
  .chart-box {
    width: 100%;
    height: 100%;
  }
  .table-box {
    box-sizing: border-box;
    padding: 6px 12px;
    width: 100%;
    height: 100%;
  }
}
.box-border {
  border: 1px solid rgba(221, 221, 221, 1);
  border-radius: 4px;
}
</style>